package com.rongshu.view.discovery.presenter;

import android.content.Context;

import com.rongshu.R;
import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.discovery.bean.SearchAuthorResponse;
import com.rongshu.view.userInfo.iview.AttentionView;
import com.widget.toast.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

public class TaAttentionPresenter extends BasePresenter<AttentionView>{

    public TaAttentionPresenter(Context context) {
        super(context);
    }

    public void getTaUserRelationships(int user_id, int ta_user_id, int type, int page, int pageSize, String sign) {
        RetorfitService.getTaUserRelationships(user_id, ta_user_id, type, page, pageSize, sign)
                .subscribe(new Subscriber<SearchAuthorResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(SearchAuthorResponse response) {
                        if (getView() != null) {
                            getView().onAttentionUser(response);
                        }
                    }
                });
    }


    //关注
    public void ConcernWith(int user_id_to) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("contribute_id", 0);
        map.put("user_id_to", user_id_to);
        RetorfitService.ConcernWith(UserInfoUtils.getUserId(), 0, user_id_to,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                if (object.getErrCode() == 0){
                    ToastUtils.show(mContext.getString(R.string.add_attention_success));
                    getView().onCancleConcern(object);
                } else {
                    ToastUtils.show(object.getErrMsg());
                }
            }
        });
    }
    //取消关注
    public void cancelConcernWith(int user_id_to) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("contribute_id", 0);
        map.put("user_id_to", user_id_to);
        RetorfitService.cancelConcernWith(UserInfoUtils.getUserId(), 0, user_id_to,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                if (object.getErrCode() == 0) {
                    ToastUtils.show(object.getErrMsg());
                    getView().onCancleConcern(object);
                } else {
                    ToastUtils.show(R.string.attention_no_permission);
                }
            }
        });
    }
}
